package com.example.s.view;

import android.app.AlertDialog;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;

import com.example.s.R;
import com.example.s.adapter.InnerHolder;
import com.example.s.utils.MyUtils;

public class MyDialog {
    private AlertDialog dialog;
    private OnMyDialogListener listener;
private boolean isOverrideCancel = false;

    public MyDialog(Context context) {
        init(context);
    }

    public MyDialog(Context context, OnMyDialogListener listener) {
        this.listener = listener;
        init(context);
    }

    public MyDialog(Context context, boolean isOverrideCancel, OnMyDialogListener listener) {
        this.listener = listener;
        this.isOverrideCancel = isOverrideCancel;
        init(context);
    }

    private void init(Context context) {
        /**
         * title
         * message
         * confirm
         * cancel
         */
        View view = LayoutInflater.from(context).inflate(R.layout.base_dialog, null);
        InnerHolder innerHolder = new InnerHolder(view);
        listener.handleDialog(innerHolder);
        dialog = new AlertDialog.Builder(context).setView(view).show();
        WindowManager.LayoutParams attributes = dialog.getWindow().getAttributes();
        attributes.width = MyUtils.getInstance().dip2px(context, 280);
        dialog.getWindow().setAttributes(attributes);
        dialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
        if(!isOverrideCancel){
        innerHolder.setOnClickListener(R.id.cancel, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                disMyDialog();
            }
        });
        }

    }

    public void disMyDialog() {
        if (dialog != null && dialog.isShowing())
            dialog.dismiss();
    }


    public interface OnMyDialogListener {
        void handleDialog(InnerHolder holder);
    }
}
